Skip to content

Conversation

@colinbennettbrain
Copy link
Contributor

  • Extract cache_read and cache_creation from LangChain's nested input_token_details object
  • Map to Braintrust's standard metric names (prompt_cached_tokens, prompt_cache_creation_tokens)

Problem

The LangChain callback handler only extracts top-level fields from usage_metadata (input_tokens, output_tokens, total_tokens) but not the nested input_token_details containing cache metrics. This causes:

  • Cache tokens showing as 0 in the experiments UI
  • Costs calculated at full uncached rate instead of discounted cache rate

direkt and others added 3 commits January 9, 2026 12:25
Extract cache_read and cache_creation from LangChain's nested
input_token_details object and map them to Braintrust's standard
metric names (prompt_cached_tokens, prompt_cache_creation_tokens).

This enables accurate cache token tracking in the UI and correct
cost calculations for cached prompts.

Fixes Pylon #10400

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@colinbennettbrain
Copy link
Contributor Author

Added a focused unit test for cache-token extraction in langchain-py.

  • New test: test_cache_token_metrics_from_input_token_details
  • It simulates LLMResult usage_metadata.input_token_details and asserts metrics include:
    prompt_cached_tokens + prompt_cache_creation_tokens
  • Ran locally: uv run pytest src/tests/test_callbacks.py -k cache_token_metrics_from_input_token_details -q

Commit: 08205f4

Copy link
Contributor

@cpinn cpinn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks great to me with the added test, was what I was going to take a look into today. Usually for changes like this we also record and save a cassette to have the tests run the tests a little faster.

cc: @ibolmo should we add a cassette to the change or should we merge as is and add it later?

@ibolmo ibolmo force-pushed the fix/langchain-cache-tokens branch from 67e1107 to 8542310 Compare January 22, 2026 01:50
@ibolmo ibolmo force-pushed the fix/langchain-cache-tokens branch from 8542310 to 546383a Compare January 22, 2026 03:04

assert "prompt_cache_creation_tokens" in first_metrics
assert first_metrics["prompt_cache_creation_tokens"] > 0
if "prompt_cached_tokens" in first_metrics:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. do we want this if statement in the test because then the assertion may or may not run?

@ibolmo ibolmo merged commit 36788d5 into main Jan 24, 2026
65 checks passed
@ibolmo ibolmo deleted the fix/langchain-cache-tokens branch January 24, 2026 02:20
@ibolmo
Copy link
Collaborator

ibolmo commented Jan 24, 2026

braintrust-langchain 0.2.1 and @braintrust/langchain-js 0.2.3 are both out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants